<template>
  <div style="padding: 0 5px;">
    <ContentWrap>
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
        <el-form-item label="报告ID" prop="id">
          <el-input v-model="queryParams.id" placeholder="请输入报告ID" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="报告名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入报告名称" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" class="mr-5px" />搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
          <el-button @click="handleDelete" type="danger" v-show="selectRow?.length">
            <Icon icon="ep:delete" />删除
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <el-table v-loading="loading" :data="list" :show-overflow-tooltip="true" @select="handleSelectTable" @select-all="handleSelectTable"
      ref="tableRef" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" align="center" prop="id" width="55" />
      <el-table-column label="名称" align="center" prop="name" >
        <template #default="scope">
          <el-link @click.stop="viewResult(scope.row)">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="执行状态" prop="exeStatus" align="center" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.exeStatus == '已完成' ? 'primary' : 'info'">{{ scope.row.exeStatus }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="执行结果" align="center" prop="resultStatus" width="100">
        <template #default="scope">
          <el-tag
            :type="scope.row.resultStatus == '成功' ? 'success' : (scope.row.resultStatus == '失败' ? 'danger' : 'info')">{{
              scope.row.resultStatus || '--' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="creator" width="100"/>
      <el-table-column label="操作时间" align="center" prop="createTime" :formatter="dateFormatter" width="200" />
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button type="danger" link @click.stop="deleteFn(scope.row.id)">
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
      @pagination="getList" />
    <viewResultDialog ref="viewResultRef" />
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as SceneApi from '@/api/scene'
import viewResultDialog from '../dialog/viewResult.vue';
import { useAppStore } from '@/store/modules/app'
import { propTypes } from '@/utils/propTypes'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const appStore = useAppStore()
const projectId = computed(() => appStore.getSelectProject)
const tableRef = ref()
const selectRow = ref()
const loading = ref(false)
const viewResultRef = ref()
const total = ref(0) // 列表的总页数
const props = defineProps({
  data: propTypes.object.def({})
})
const queryParams = reactive({
  id: '',
  name: '',
  pageNo: 1,
  pageSize: 10,
  sceneId: props.data.id,
  projectId: projectId.value
})
const list = ref([] as any)
const queryFormRef = ref()

/** 获取执行列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SceneApi.getSceneReportList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
defineExpose({ getList })

/** 搜索 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除 */
const deleteFn = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SceneApi.deleteSceneReport({ id })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}
const handleDelete = async () => {
  try {
    if (!selectRow.value.length) {
      message.warning('请选择需要删除的数据')
      return
    }
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    const ids = selectRow.value.map(s => s.id)
    await SceneApi.deleteBatchSceneReport(ids)
    message.success(t('common.delSuccess'))
    selectRow.value = []
    // 刷新列表
    await getList()
  } catch { }

}
/** 点击序号，打开报告弹窗 */
const viewResult = (row) => {
  viewResultRef.value.open(row)
}
/** 导出 */
const exportFn = (id) => {

}

/** 选中表格 */
const handleSelectTable = () => {
  selectRow.value = tableRef.value.getSelectionRows()
}
/** 行点击 */
const handleRowClick = (row) => {
  viewResultRef.value.open(row)
}
onMounted(() => {
})
</script>

<style scoped lang="scss">
.row {
  p {
    display: flex;

    span {
      margin: 0 5px;
    }
  }
}

:deep(th.el-table__cell) {
  font-weight: 400;
}
</style>
